<ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../Brushes.xaml"/>
  </ResourceDictionary.MergedDictionaries>

  <!-- CONTEXT MENU XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->
  <DropShadowBitmapEffect x:Key="PopupDropShadow" ShadowDepth="3" Softness="0.25" Opacity="0.3" Color="White" />
  <Style TargetType="{x:Type ContextMenu}">
    <Style.Resources>
      <Style x:Key="MenuScrollButton" BasedOn="{x:Null}" TargetType="{x:Type RepeatButton}">
        <Setter Property="ClickMode" Value="Hover" />
        <Setter Property="MinWidth" Value="0" />
        <Setter Property="MinHeight" Value="0" />
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type RepeatButton}">
              <DockPanel SnapsToDevicePixels="true" Background="Transparent">
                <Rectangle Fill="Transparent" x:Name="R1" Width="1" DockPanel.Dock="Right" />
                <Rectangle Fill="Transparent" x:Name="B1" Height="1" DockPanel.Dock="Bottom" />
                <Rectangle Fill="Transparent" x:Name="L1" Width="1" DockPanel.Dock="Left" />
                <Rectangle Fill="Transparent" x:Name="T1" Height="1" DockPanel.Dock="Top" />
                <ContentPresenter HorizontalAlignment="Center" Margin="2,2,2,2" x:Name="ContentContainer" VerticalAlignment="Center" />
              </DockPanel>
              <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="true">
                  <Setter Property="Fill" TargetName="R1" Value="{StaticResource FocusScrollButtonBrush}" />
                  <Setter Property="Fill" TargetName="B1" Value="{StaticResource FocusScrollButtonBrush}" />
                  <Setter Property="Fill" TargetName="L1" Value="{StaticResource FocusScrollButtonBrush}" />
                  <Setter Property="Fill" TargetName="T1" Value="{StaticResource FocusScrollButtonBrush}" />
                  <Setter Property="Margin" TargetName="ContentContainer" Value="3,3,1,1" />
                </Trigger>
              </ControlTemplate.Triggers>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
      <Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry>
      <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter" />
      <Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry>
      <Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry>

      <Style x:Key="{ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}" BasedOn="{x:Null}" TargetType="{x:Type ScrollViewer}">
        <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
        <Setter Property="VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
              <Grid SnapsToDevicePixels="true">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto" />
                  <RowDefinition Height="*" />
                  <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Border Grid.Row="1">
                  <ScrollContentPresenter Margin="{TemplateBinding Padding}" />
                </Border>
                <RepeatButton Style="{StaticResource MenuScrollButton}"
                              Focusable="false" Command="{x:Static ScrollBar.LineUpCommand}"
                              CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                  <RepeatButton.Visibility>
                    <MultiBinding FallbackValue="Visibility.Collapsed" Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="0">
                      <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}" />
                      <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}" />
                      <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                      <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                    </MultiBinding>
                  </RepeatButton.Visibility>
                  <Path Fill="{StaticResource ScrollButtonBrush}" Data="{StaticResource UpArrow}" />
                </RepeatButton>
                <RepeatButton Style="{StaticResource MenuScrollButton}" Focusable="false" Grid.Row="2" Command="{x:Static ScrollBar.LineDownCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                  <RepeatButton.Visibility>
                    <MultiBinding FallbackValue="Visibility.Collapsed" Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="100">
                      <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}" />
                      <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}" />
                      <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                      <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                    </MultiBinding>
                  </RepeatButton.Visibility>
                  <Path Fill="{StaticResource ScrollButtonBrush}" Data="{StaticResource DownArrow}" />
                </RepeatButton>
              </Grid>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>

      <!-- Style the area of each sub-menu in which items are placed -->
      <ControlTemplate x:Key="{ComponentResourceKey ResourceId=ContextmenuContent, TypeInTargetAssembly={x:Type FrameworkElement}}"
                       TargetType="{x:Type ContentControl}">
        <Border Background="{StaticResource MenuBackground}"
                BorderBrush="{StaticResource MenuBorderBrush}"
                BorderThickness="1"
                CornerRadius="0,0,6,0">
          <Grid>
            <ContentPresenter Margin="1,0" Grid.ColumnSpan="2" />
          </Grid>
        </Border>
      </ControlTemplate>
      </Style.Resources>

    <!-- Setter Property="Background" Value="{DynamicResource MenuBackgroundBrush}"/ -->
    <!-- Setter Property="BorderThickness" Value="1"/ -->
    <!-- Setter Property="BorderBrush" Value="{DynamicResource WindowBorderBrush}"/ -->
    <Setter Property="HasDropShadow" Value="True" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ContextMenu}">
          <!-- Style ContextMenu such that Drop shadow is visible through Grid with Margin 0,0,3,3 -->
          <Grid x:Name="SubMenu" Margin="0,0,3,3" SnapsToDevicePixels="true">
            <!-- Implement drop shadow through border with bitmap effect -->
            <Border x:Name="Shdw"
                      Background="{StaticResource MenuBackground}"
                      CornerRadius="0,0,6,0"
                      BorderBrush="Transparent"
                      BorderThickness="0"
                      BitmapEffect="{DynamicResource PopupDropShadow}">
              <Grid Margin="0">
                <ContentControl x:Name="SubMenuBorder"
                                    IsTabStop="false"
                                    Template="{DynamicResource {ComponentResourceKey ResourceId=ContextmenuContent, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                  <ScrollViewer Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"
                                    CanContentScroll="true">
                    <Grid>
                      <!-- Display nice icon column enhancement (even if there is no icon to be displayed) -->
                      <Border Margin="0,0,0,0"
                              Width="36"
                              Background="{StaticResource MenuDecorationBarGradient}"
                              HorizontalAlignment="Left"
                              VerticalAlignment="Stretch" />

                      <ItemsPresenter Margin="2"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      Grid.IsSharedSizeScope="true"
                                      KeyboardNavigation.DirectionalNavigation="Cycle"
                                      KeyboardNavigation.TabNavigation="Cycle" />
                    </Grid>
                  </ScrollViewer>
                </ContentControl>
              </Grid>
            </Border>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

</ResourceDictionary>
